var toolBarRoadName = "",
    startRoad4Select = "",
    endRoad4Select = "",
    roadLineProvince = 0,
    roadLineCity = 0,
    roadLineArea = 0,
    roadLineFlag = 0,
    coordinateFlag = 0,//判断坐标集有无更新
    map_type = 3;
var myDate = new Date();
var date = myDate.getFullYear() + "-" +
    myDateFormat(myDate.getMonth() + 1) + "-" +
    myDateFormat(myDate.getDate());
var currentActorId = 3;
var localOrgId = 0;
if (gUserLoginResult.userActors.indexOf("7") >= 0) currentActorId = 7;
if (gUserLoginResult.userActors.indexOf("1") >= 0) currentActorId = 1;

function myDateFormat(s) {
    return s < 10 ? '0' + s : s;
}

var dex = 0;//图片轮转基准  每次打开置0

var localActorId;
$(function () {
    localOrgId = gUserLoginResult.orgId;
    localActorId = currentActorId;
    gMainDT = $("#mainDataTable").DataTable({
        ajax: {
            url: "../rds_roadLineController/getRoadLine4DT",
            type: "POST",
            data: function (d) {
                d.search = $('#mainDataTable').DataTable().search(this.value);
                d.actorIds = localActorId;
                d.orgId = localOrgId;
                d.audit = false;
                d.startRoad = startRoad4Select;
                d.endRoad = endRoad4Select;
                d.roadName = toolBarRoadName;
            }
        },
        columns: [{
            data: "roadLineId",
            render: function (data, type, row, meta) {
                var checkId = "checkbox" + (parseInt(meta.row) + 1);
                return "<input type='checkbox' value='" + data + "' id='" + checkId + "'>";
            }
        }, {
            data: null,
            targets: 0
        }, {
            data: "roadLinePhoto",
            render: function (data, type, row) {
                var pic = data.split(",");
                if (pic[0])
                    return "<img src='http://hldoss.oss-cn-hangzhou.aliyuncs.com/" + pic[0] + "' style='width: 50px; height: 40px; cursor: pointer' " +
                        "onclick='$.showPicModal(\"" + data + "\")'>";
                else
                    return "<img src='../images/default_photo.png' style='width: 50px; height: 40px; cursor: pointer'>";
            }
        }, {
            data: "roadLineId",
            render: function (data, type, row) {
                var str = "<button id='showMap' " +
                    "onclick='$.showMap(" + data + ")' " +
                    "class='btn  btn-primary  btn-xs btn-flat' title='地图位置'>" +
                    "<i class='glyphicon glyphicon-map-marker'></i></button> ";
                str += row.roadLineProvince + " " + row.roadLineCity + " " + row.roadLineArea;
                return str;
            }
        }, {
            data: "roadLineRoadName"
        }, {
            data: null,
            render: function (data, type, row) {
                return row.roadLineStartRoad + "——" + row.roadLineEndRoad;
            }
        }, {
            data: "roadLineOrgName"
        }, {
            data: "roadLineCreatorName"
        }, {
            data: "roadLineInstallTime"
        }, {
            data: "roadLineId",
            render: function (data, type, row) {
                return "<button class='btn btn-primary btn-xs btn-flat' " +
                    "onclick='$.showInfo(" + JSON.stringify(row) + ")' title='信息查看'>" +
                    "<i class='glyphicon glyphicon-eye-open'></i></button> " +
                    "<button class='btn btn-primary btn-xs btn-flat' " +
                    "onclick='$.showQrcode(" + JSON.stringify(row) + ")' title='二维码'>" +
                    "<i class='glyphicon glyphicon-qrcode'></i></button>";
            }
        }, {
            data: "roadLineId",
            render: function (data, type, row) {
                var buttonOperation = "";
                if (row.roadLineIsComplete == 1)
                    buttonOperation += "<button  class='btn  btn-success btn-xs btn-flat' title='已审核'>" +
                        "<i class='fa  fa-check-square-o'></i></button> ";
                else
                    buttonOperation += "<button  class='btn  btn-danger btn-xs btn-flat' title='未审核'>" +
                        "<i class='fa fa-square-o'></i></button> ";

                var buttonUpdate = "<button class='btn btn-primary btn-xs btn-flat' " +
                    "onclick='$.showUpdateRoadLine(" + JSON.stringify(row) + ")' title='编辑'>" +
                    "<i class='fa fa-edit'></i></button> ";
                gUpdate == 0 ? buttonOperation : buttonOperation += buttonUpdate;

                var buttonDelete = "<button class='btn btn-primary btn-xs btn-flat' " +
                    "onclick='$.showDeleteRoadLine(" + data + ")' title='删除'>" +
                    "<i class='fa fa-minus-square'></i></button>";
                gDelete == 0 ? buttonOperation : buttonOperation += buttonDelete;

                return buttonOperation == "" ? buttonOperation = "无权限" : buttonOperation;
            }
        }],
        dom: "<'row'<'col-md-2'l><'#mainToolbar.col-md-10'>r>t<'row'<'col-md-5'i><'col-md-7'p>>",
        initComplete: function () {
            // 下拉工具栏按钮的权限
            $("#mainToolbar").empty();
            var strToolBar = "<div class='pull-right'><div class='btn-group'>" +
                "<button type='button' class='btn btn-primary btn-sm btn-flat'><i class='fa fa-cog'></i> 操作</button>" +
                "<button type='button' class='btn btn-primary btn-sm btn-flat dropdown-toggle' " +
                "data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>" +
                "<span class='caret'></span><span class='sr-only'>切换下拉</span></button>" +
                "<ul class='dropdown-menu'>" +
                "<li>" +
                "<button href='#' class='btn btn-primary btn-sm btn-flat btn-block' " +
                "onclick='gMainDT.ajax.reload(null, false);'><i class='fa fa-refresh'></i> 刷新</button>" +
                "</li>";

            var buttonAdd = "<li><button href='#' class='btn btn-primary btn-sm btn-flat btn-block' " +
                "onclick='$.showAddRoadLine()'><i class='fa fa-plus-square'></i> 添加</button></li>";
            gInsert == 0 ? strToolBar : strToolBar += buttonAdd;
            var buttonDelete = "<li><button href='#' class='btn btn-primary btn-sm btn-flat btn-block' " +
                "onclick='$.showDeleteRoadLine(0)'><i class='fa fa-minus-square'></i> 删除</button></li>";
            gDelete == 0 ? strToolBar : strToolBar += buttonDelete;

            var html2 = "";
            if(currentActorId == 1)
                html2 += "<select id='orgId4Select' style='max-width: 280px' onchange='$.getRoadLineStartRoad()' class='form-control'>" + gOrgListHtml +"</select>&nbsp;";
            strToolBar += "</ul></div> " +
                "<div class='input-group-sm' style='display:inline-block'>" + html2 +
                "<input id='toolBarRoadName' onkeyup='$getRoadLineStartRoad()'  type='text' class='form-control mysize' placeholder='路名'>" +
                "&nbsp;<select   style='width: 80px' onchange='$.getRoadLineEndRoad()' class='form-control mysize' id='startRoad4Select'>" +
                "<option value=''>起/止</option>" +
                "</select>&nbsp;" +
                "<select class='form-control mysize'  style='width: 80px' id='endRoad4Select'>" +
                "<option value=''>止/起</option>" +
                "</select>&nbsp;" +
                "<input type='text' class='form-control' style='width: 80px' id='searchText' placeholder='项目编号'>" +
                "<button class='btn btn-primary btn-flat btn-sm' type='button' onclick='$(this).searchDT()'>" +
                "<i class='fa fa-search'></i></button></div></div>";

            $("#mainToolbar").append(strToolBar);
            $.getRoadLineStartRoad();
        },
        processing: true,
        serverSide: true,
        ordering: false,
        select: false,
        autoFill: true,
        displayLength: 20,
        lengthMenu: [[5, 10, 15, 20, 50, 100, 150, -1], [5, 10, 15, 20, 50, 100, 150, "All"]],
        language: {
            url: "zh_cn.json"
        },
        fnDrawCallback: function () {// 序号自增回调函数
            var api = this.api();
            var startIndex = api.context[0]._iDisplayStart;
            api.column(1).nodes().each(function (cell, i) {
                cell.innerHTML = startIndex + i + 1;
            });
        }
    });

    var oTable = $('#mainDataTable').dataTable();
    if (gUserLoginResult.userActors.indexOf('1') > -1) {
        oTable.fnSetColumnVis(5, false);
        oTable.fnSetColumnVis(7, false);
    } else if (gUserLoginResult.userActors.indexOf('7') > -1) {
        oTable.fnSetColumnVis(0, false);
        oTable.fnSetColumnVis(5, false);
        oTable.fnSetColumnVis(7, false);
        oTable.fnSetColumnVis(9, false);
    } else {
        oTable.fnSetColumnVis(6, false);
    }


});

var gActorId = gUserLoginResult.userActors.includes("1") ? 1 : 2;
$.getOrgList = function () {
    var html = "", result = "";
    $.ajax({
        url: "../rds_fault/getOrg4Select",
        type: "post",
        async: false,
        data: {
            actorId: gActorId,
            orgId: gUserLoginResult.orgId
        },
        success: function (msg) {
            if (msg) {
                if (msg.isSuccess) {
                    var jsonData = msg.jsonData;
                    var orglist = "";
                    if (jsonData.length == 0) {
                        firstOrgId = gUserLoginResult.orgId;
                        result = "<option value='" + gUserLoginResult.orgId + "'>" + gUserLoginResult.orgName + "</option>"
                    }
                    else {

                        for (var i in jsonData) {
                            orglist += "," + jsonData[i].orgId;
                            html += "<option value='" + jsonData[i].orgId + "'>" + jsonData[i].orgName + "</option>"
                        }
                        orglist = orglist.substr(1);
                        if (gActorId == 1) {
                            result += "<option value='-2'>全部</option>" +
                                "<option value='-1'>未设置</option>"
                            firstOrgId = -2;
                        }
                        else {
                            firstOrgId = orglist
                            result += "<option value='" + orglist + "'>全部</option>"
                        }
                        result += html;
                    }

                }

            }
        }
    })
    return result;
}

var gOrgListHtml = $.getOrgList();

$.showInfo = function (jsonDate) {
    $(".no_act").show();
    $("#defaultFooter").hide();
    $("#specialFooter").show();
    $.showUpdateRoadLine(jsonDate, 3);
};

$.beDefault = function () {
    $(".no_act").hide();
    $("#defaultFooter").show();
    $("#specialFooter").hide();
};

$.showPicModal = function (picList) {
    dex = 0;//图片轮转置0
    picList = picList.split(",");
    var pic = $('.img-content');
    pic.html("").css("top", "0px");
    for (var i in picList) {
        if (picList[i] != "")
            pic.append("<img src='http://hldoss.oss-cn-hangzhou.aliyuncs.com/" + picList[i] + "'>")
    }
    $(".img-content img[src]:first").show();
    $("#showPicList").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    })
};

$.getRoadLineStartRoad = function () {
    var mOrgId = $("#orgId4Select").val();
    var localOrg = gUserLoginResult.orgId;
    var mActorId = currentActorId;
    if (mActorId != 7) {
        if (mOrgId == -2) {
            mActorId = 1;
        } else if(mOrgId){
            mActorId = 3;
            localOrg = mOrgId;
        }
    }
    $.ajax({
        url: "../rds_roadLineController/getStartRoadByRoadName",
        type: "POST",
        data: {
            "roadName": $("#toolBarRoadName").val(),
            "actorIds": mActorId,
            "orgId": localOrg
        },
        success: function (msg) {
            if (msg != null && typeof(msg) != "undefined") {
                if (msg.isSuccess == true) {
                    var roadNames = msg.jsonData;
                    $("#startRoad4Select").empty();
                    if (roadNames.length != 0) {
                        // orgDataFlag = 0;
                        $("#startRoad4Select").append("<option value=''>起/止</option>");
                        for (var i = 0; i < roadNames.length; i++)
                            $("#startRoad4Select").append("<option value='" + roadNames[i] + "'>" +
                                roadNames[i] + "</option>");
                    }
                    else
                        $("#startRoad4Select").append("<option>无</option>");
                } else {
                    $.alert("获取起始道路数据失败", 2);
                }
            }
        }
    })
};

function _debounce(fn, delay) {
    var timer
    return function () {
        var context = this
        var args = arguments
        clearTimeout(timer)
        timer = setTimeout(function () {
            fn.apply(context, args)
        }, delay)
    }
}

var $getRoadLineStartRoad = _debounce($.getRoadLineStartRoad, 1000);

$.Trim = function (str, is_global) {
    var result;
    result = str.replace(/(^\s+)|(\s+$)/g, "");
    if (is_global.toLowerCase() == "g") {
        result = result.replace(/\s/g, "");
    }
    return result;
};

$.getRoadLineEndRoad = function () {
    $.ajax({
        url: "../rds_roadLineController/getEndRoadByStartRoad",
        type: "POST",
        data: {
            "roadLineRoadName": $.Trim($("#toolBarRoadName").val(), "g"),
            "startRoad": $("#startRoad4Select").val(),
            "actorIds": gUserLoginResult.userActors,
            "orgId": gUserLoginResult.orgId
        },
        success: function (msg) {
            if (msg != null && typeof(msg) != "undefined") {
                if (msg.isSuccess == true) {
                    var roadNames = msg.jsonData;
                    $("#endRoad4Select").empty();
                    if (roadNames.length != 0) {
                        // orgDataFlag = 0;
                        $("#endRoad4Select").append("<option value=''>起/止</option>");
                        for (var i = 0; i < roadNames.length; i++)
                            $("#endRoad4Select").append("<option value='" + roadNames[i] + "'>" +
                                roadNames[i] + "</option>");
                    }
                    else
                        $("#endRoad4Select").append("<option>无</option>");
                } else {
                    $.alert("获取起始道路数据失败", 2);
                }
            }
        }
    })
}


$.fn.searchDT = function () {
    toolBarRoadName = $.Trim($("#toolBarRoadName").val(), "g");
    startRoad4Select = $("#startRoad4Select").val();
    endRoad4Select = $("#endRoad4Select").val();
    localOrgId = $("#orgId4Select").val();
    if (localActorId != 7) {
        if (localOrgId == -2) {
            localActorId = 1;
        } else {
            localActorId = 3;
        }
    }
    if(!localOrgId)
    {
        localOrgId = gUserLoginResult.orgId;
    }
    gMainDT.search($("#searchText").val()).draw();
};

$.checkChange = function () {
    var isChecked = $("#batchCheck").prop('checked');
    $("input:checkbox[id^='checkbox']").prop("checked", isChecked);
};

$.showMap = function (jsonData) {
    $.ajax({
        url: "../rds_roadLineController/getCoordinatesById",
        type: "post",
        data: {
            roadLineId: jsonData,
            type: 1
        },
        success: function (msg) {
            if (msg != null && typeof msg != "undefined") {
                if (msg.isSuccess == true) {
                    var points = "", josnData = msg.jsonData;
                    josnData.forEach(function (value, index, array) {
                        points += value.coordinateLongitude + "," + value.coordinateLatitude + ";";
                    });
                    $("#modalMapRoad").hide();
                    var url = "management/roadLineMap.html";
                    url += "?coordinate=" + points + "&type=3";//通过这种方式传参数
                    $("#myMap").attr("src", url);
                    $("#modal-map").modal({
                        backdrop: 'static',
                        keyboard: false,
                        show: true
                    })
                }
            }
        }
    });
};

$.showHistory = function (roadLineId) {
    historyDt = $("#historyDataTable").DataTable({
        ajax: {
            url: "../rds_roadSignManageController/getHistory4DT",
            type: "POST",
            data: function (d) {
                d.search = $('#historyDataTable').DataTable().search(this.value);
                d.equipmentId = roadLineId;
                d.equipmentType = 6;
            }
        },
        columns: [{
            data: null,
            targets: 0
        }, {
            data: "eventType"
        }, {
            data: "operatingTime"
        }, {
            data: "operatingPerson"
        }],
        dom: "<'row'<'col-md-6'><'#historyTool.col-md-6'>r>t<'row'<'col-md-5'i><'col-md-7'p>>",
        initComplete: function () {
            $("#historyTool").empty();
            var strToolBar = "<div class='input-group input-group-sm'>" +
                "<input type='text' class='form-control' id='datePicker' placeholder='操作时间'>" +
                "<span class='input-group-btn'>" +
                "<button class='btn btn-primary btn-flat' type='button' " +
                "onclick='$(this).searchInfoInHistoryDt()'><i class='fa fa-search'></i>" +
                "</button></span></div>";
            $("#historyTool").append(strToolBar);
            $("#datePicker").bind('keydown', function (event) {
                if (event.keyCode == "13") {
                    $(this).searchInfoInHistoryDt();
                }
            });
            $('#datePicker').datepicker({
                format: 'yyyy-mm-dd',
                autoclose: true,
                todayBtn: 'linked',
                language: 'zh-CN'
            });
        },
        processing: true,
        serverSide: true,
        ordering: false,
        select: false,
        autoFill: true,
        destroy: true,
        displayLength: 10,
        lengthMenu: [[5, 10, 15, 20, 50, 100, 150, -1], [5, 10, 15, 20, 50, 100, 150, "All"]],
        language: {
            url: "zh_cn.json"
        },
        fnDrawCallback: function () {// 序号自增回调函数
            var api = this.api();
            var startIndex = api.context[0]._iDisplayStart;
            api.column(0).nodes().each(function (cell, i) {
                cell.innerHTML = startIndex + i + 1;
            });
        }
    });

    $("#historyModal").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    });
};

$.fn.searchInfoInHistoryDt = function () {
    var search = $("#datePicker").val().replace(/'/g, '');
    historyDt.search(search).draw();
};

$.showQrcode = function (jsonData) {
    $("#qrcode").empty();
    var qrcode = new QRCode(document.getElementById("qrcode"), {
        width: 200,
        height: 200
    });
    qrcode.makeCode("orgId:" + jsonData.roadLineOrgId + ",roadLineId:" + jsonData.roadLineId);
    $("#qrcodeModal").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    });
};

$("#province").change(function () {
    roadLineProvince = $("#province").val();
    $.getRegion("city", $("#province").val());
    $("#city").val(parseInt($("#province").val()) + 100);
    roadLineCity = $("#city").val();
    $.getRegion("area", $("#city").val());
    $("#area").val(parseInt($("#city").val()) + 1);
    roadLineArea = $("#area").val();
});

$("#city").change(function () {
    roadLineCity = $("#city").val();
    $.getRegion("area", $("#city").val());
    $("#area").val(parseInt($("#city").val()) + 1);
    roadLineArea = $("#area").val();
});

$("#area").change(function () {
    roadLineArea = $("#area").val();
});

$.getRegion = function (id, pid) {
    $.ajax({
        url: "../rds_roadSignManageController/getRegion4Select",
        type: 'post',
        async: false,
        dataType: 'json',
        data: {
            'pid': pid
        },
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    var provinceData = msg.jsonData;
                    $("#" + id).empty();
                    if (provinceData.length != 0) {
                        provinceDataFlag = 0;
                        for (var i = 0; i < provinceData.length; i++)
                            $("#" + id).append("<option value='" + provinceData[i].regionId + "'>" +
                                provinceData[i].regionName + "</option>");
                    }
                    else {
                        provinceDataFlag = 1;
                        $("#" + id).append("<option value='" + gUserLoginResult.regionId + "'>" +
                            gUserLoginResult.regionName + "</option>");
                    }
                } else {
                    $.alert("获取地区列表失败", 2);
                }
            } else {
                $.alert('系统错误', 2);
            }
        },
        error: function () {
            $.alert("系统错误", 2);
        }
    });
};


$.getMainOrgNameAndId = function (orgId) {
    $.ajax({
        url: "../rds_org/getMainOrgNameAndId",
        type: 'post',
        async: false,
        dataType: 'json',
        data: {
            'orgId': orgId
        },
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    var orgData = msg.jsonData;
                    $("#roadLineMainOrgId").empty();
                    if (orgData.length != 0) {
                        orgDataFlag = 0;
                        for (var i = 0; i < orgData.length; i++)
                            $("#roadLineMainOrgId").append("<option value='" + orgData[i].orgId + "'>" +
                                orgData[i].orgName + "</option>");
                    } else {
                        orgDataFlag = 1;
                        $("#roadLineMainOrgId").append("<option value='" + gUserLoginResult.orgId + "'>" +
                            gUserLoginResult.orgName + "</option>");
                    }
                } else {
                    $.alert("获取监管单位数据失败", 2);
                }
            } else {
                $.alert('系统错误', 2);
            }
        },
        error: function () {
            $.alert("系统错误", 2);
        }
    });
};

$("#myBtn").click(function () {
    coordinateFlag = 1;//判断坐标集有无改变
    $("#modalMapRoad").show();
    $("#startRoad").val($("#roadLineStartRoad").val());
    $("#endRoad").val($("#roadLineEndRoad").val());
    var url = "management/roadLineMap.html";
    var coo = $("#coordinate").val(),
        coordinate;
    if (coo != "") {
        coordinate = coo;
    } else {
        coordinate = $("#city").find("option:selected").text() + ","
            + $("#area").find("option:selected").text();
        map_type = 0;//无值
    }
    url += "?coordinate=" + coordinate + "&type=" + map_type + "";//通过这种方式传参数
    $("#myMap").attr("src", url);
    $("#modal-map").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    })
});

$("#subBtn").click(function () {
    $("#roadLineStartRoad").val($("#startRoad").val());
    $("#roadLineEndRoad").val($("#endRoad").val());
    if ($("#roadLinePoints").val() == sessionStorage.getItem("points") && sessionStorage.getItem("flag") == 0)
        $("#roadLinePoints").val("no");//判断坐标集有无更新
    else
        $("#roadLinePoints").val(sessionStorage.getItem("points"));
    $("#coordinate").val(sessionStorage.getItem("points"));
    $("#roadLineRoadName").val(sessionStorage.getItem("roadName"));
    //$("#coordinate").val(sessionStorage.getItem("x") + "," + sessionStorage.getItem("y"));
    $('#modal-map').modal("hide");
    console.log($("#roadLinePoints").val())
});

$('#roadLineInstallTime').datepicker({
    format: 'yyyy-mm-dd',
    autoclose: true,
    todayBtn: 'linked',
    language: 'zh-CN'
});

$.openUpPic = function () {
    $("#upPictureModal").modal({
        backdrop: 'static',
        keyboard: true,
        show: true
    });
};

$.closeUpPhModal = function () {
    $("#upPictureModal").modal("hide");
    $("#ossfile").empty();
    clearOssData();
};

$.doneUpPhoto = function () {
    var fileInfo = getOssFileInfo();
    console.log(fileInfo);
    if ($(".pic_list img:first").attr("src").indexOf("default_photo") > 0)
        $(".pic_list").html("");
    var urls = [];
    for (var i in fileInfo) {
        if (fileInfo[i].fileUrl) {
            $(".pic_list").append("<img src='" + fileInfo[i].fileUrl + "'>");
            urls.push(fileInfo[i].fileKey);
        }
    }
    urls = urls.join(",");
    if ($("#roadLinePhoto").val())//原有图片 后续添加
        $("#roadLinePhoto").val($("#roadLinePhoto").val() + "," + urls);
    else
        $("#roadLinePhoto").val(urls);
    $.closeUpPhModal();
};

function judgeFileType(suffix) {
    //图片文件
    if (!(suffix == ".jpg" || suffix == ".JPG" || suffix == ".png" || suffix == ".PNG" ||
            suffix == ".jpeg" || suffix == ".JPEG" || suffix == ".gif" || suffix == ".GIF" ||
            suffix == ".bmp" || suffix == ".BMP")) {
        alert("请选择图片文件！");
        return false;
    }
}


$.showAddRoadLine = function () {
    $("#graphic").show();

    roadLineFlag = 0;
    $(".pic_list").css("top", "0px")
    $(".pic_list").html('<img id="roadLinePhotoImg"  src="../images/default_photo.png">')
    sessionStorage.setItem("flag", 0);
    $.getRegion("province", 0);
    if (roadLineProvince == 0)
        $("#province").val(330000);
    else
        $("#province").val(roadLineProvince);
    $.getRegion("city", $("#province").val());
    if (roadLineCity == 0)
        $("#city").val(330200);
    else
        $("#city").val(roadLineCity);
    $.getRegion("area", $("#city").val());
    if (roadLineArea == 0)
        $("#area").val(330211);
    else
        $("#area").val(roadLineArea);

    $("#roadLineInstallTime").val(date);
    $("#roadLineOrgName").val(gUserLoginResult.orgName);
    $("#roadLinePeriod").val(1);
    $("#roadLineServiceLife").val(1);
    $.getMainOrgNameAndId(gUserLoginResult.orgId);
    $("#roadLineType").val(0);
    $("#roadLineGraphic").val(0);
    $("#roadLineCreatorName").val(gUserLoginResult.userName);
    $("#roadLineCreatorId").val(gUserLoginResult.userId);
    $("#roadLineOrgId").val(gUserLoginResult.orgId);
    // $("#roadLinePhoto").val("../images/default_photo.png");

    $("#roadLineLabel").text("添加标线信息");
    $("#roadLineModal").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    });
};

$.showUpdateRoadLine = function (jsonData) {
    coordinateFlag = 0;
    roadLineFlag = 1;
    map_type = (arguments[1]) ? arguments[1] : 1;
    $("#roadLineLabel").text(map_type == 1 ? "编辑信息" : "信息展示");//展示or编辑
    sessionStorage.setItem("flag", 0);
    $.ajax({
        url: "../rds_roadLineController/getCoordinatesById",
        type: "post",
        data: {roadLineId: jsonData.roadLineId, type: 1},
        success: function (msg) {
            if (msg != null && typeof msg != "undefined") {
                if (msg.isSuccess == true) {
                    var points = "", josnData = msg.jsonData;
                    josnData.forEach(function (value, index, array) {
                        points += value.coordinateLongitude + "," + value.coordinateLatitude + ";";
                    });
                    $("#coordinate").val(points);
                    $("#roadLinePoints").val(points);
                }
            }
        }
    });


    dex = 0;//图片轮转
    var picList = jsonData.roadLinePhoto.split(",");
    var $pic = $(".pic_list"), flag = 0;
    $pic.html("").css("top", "0px");
    for (var i in picList) {
        if (picList[i]) {
            flag = 1;
            $pic.append("<img src='http://hldoss.oss-cn-hangzhou.aliyuncs.com/" + picList[i] + "'>");
        }
    }
    if (flag == 0)
        $pic.append("<img src='../images/default_photo.png'>");

    $("#projectNumber").val(jsonData.projectNumber);
    $.getRegion("province", 0);
    $("#province").val(jsonData.roadLineRegionId.toString().substring(0, 2) + "0000");
    $.getRegion("city", $("#province").val());
    $("#city").val(jsonData.roadLineRegionId.toString().substring(0, 4) + "00");
    $.getRegion("area", $("#city").val());
    $("#area").val(jsonData.roadLineRegionId);

    $("#roadLineRoadName").val(jsonData.roadLineRoadName);
    $("#roadLineStartRoad").val(jsonData.roadLineStartRoad);
    $("#roadLineEndRoad").val(jsonData.roadLineEndRoad);
    $("#roadLineBrand").val(jsonData.roadLineBrand);
    $("#roadLineInstallOrg").val(jsonData.roadLineInstallOrg);
    $("#roadLineInstallTime").val(jsonData.roadLineInstallTime);
    $("#roadLineRemark").val(jsonData.roadLineRemark);
    $("#roadLineOrgName").val(jsonData.roadLineOrgName);
    //  $("#roadLinePhotoImg").attr("src", jsonData.roadLinePhoto);
    $("#roadLinePeriod").val(jsonData.roadLinePeriod);
    $("#roadLineServiceLife").val(jsonData.roadLineServiceLife);
    $.getMainOrgNameAndId(gUserLoginResult.orgId);
    orgDataFlag == 0 ?
        $("#roadLineMainOrgId").val(jsonData.roadLineMainOrgId) :
        $("#roadLineMainOrgId").val(gUserLoginResult.orgId);
    $("#roadLineType").val(jsonData.roadLineType);
    setHtml(jsonData.roadLineType);
    $("#roadLineGraphic").val(jsonData.roadLineGraphic);
    $("#roadLineId").val(jsonData.roadLineId);
    $("#roadLineCreatorName").val(jsonData.roadLineCreatorName);
    $("#roadLineCreatorId").val(jsonData.roadLineCreatorId);
    $("#roadLineModifierId").val(gUserLoginResult.userId);
    $("#roadLineOrgId").val(jsonData.roadLineOrgId);
    $("#roadLinePhoto").val(jsonData.roadLinePhoto);

    $("#roadLineMileage").val(jsonData.roadLineMileage);
    $("#roadLineModal").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    });
};

$('#roadLineForm').bootstrapValidator({
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        projectNumber: {
            validators: {
                stringLength: {max: 20, message: '项目编号长度不超过20'}
            }
        },
        coordinate: {
            validators: {
                notEmpty: {message: '坐标不能为空'}
            }
        },
        roadLineRoadName: {
            validators: {
                notEmpty: {message: '路名不能为空'},
                stringLength: {max: 20, message: '路名长度不超过20'}
            }
        },
        roadLineStartRoad: {
            validators: {
                notEmpty: {message: '起始路口不能为空'},
                stringLength: {max: 20, message: '路名长度不超过20'}
            }
        },
        roadLineEndRoad: {
            validators: {
                notEmpty: {message: '结束路口不能为空'},
                stringLength: {max: 20, message: '路名长度不超过20'}
            }
        },
        roadLineBrand: {
            validators: {
                stringLength: {max: 20, message: '品牌长度不超过20'}
            }
        },
        roadLineInstallOrg: {
            validators: {
                stringLength: {max: 50, message: '安装公司长度不超过50'}
            }
        },
        roadLineInstallTime: {
            validators: {
                stringLength: {max: 50, message: '安装日期长度不超过50'}
            }
        },
        roadLineRemark: {
            validators: {
                stringLength: {max: 200, message: '说明备注长度不超过200'}
            }
        }
    }
}).on('success.form.bv', function (e) {
    e.preventDefault();
    $("#roadLineProvince").val($("#province").find("option:selected").text());
    $("#roadLineCity").val($("#city").find("option:selected").text());
    $("#roadLineArea").val($("#area").find("option:selected").text());
    $("#roadLineRegionId").val($("#area").val());
    if (coordinateFlag == 0)
        $("#roadLinePoints").val("no");
    var $form = $(e.target);
    var url;
    var formData = $form.serialize();
    if (roadLineFlag == 0)
        url = "../rds_roadLineController/addRoadLine";
    else
        url = "../rds_roadLineController/updateRoadLine";
    $.ajax({
        url: url,
        type: 'post',
        data: formData,
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    $.alert("数据操作成功", 1);
                    gMainDT.ajax.reload(null, false);
                } else {
                    $.alert("数据操作失败", 2);
                }
            } else {
                $.alert('系统错误，请联系管理员', 2);
            }
        },
        error: function () {
            $.alert('系统错误，请联系管理员', 2);
        }
    });
    $.clearModal($("#roadLineForm"), $("#roadLineModal"));
});

$("#roadLineInstallTime").blur(function () {
    $("#roadLineInstallTime").val() == "" ? $("#roadLineInstallTime").val(date) : null;
});

$.showDeleteRoadLine = function (roadLineId) {
    var count = 1;
    if (roadLineId == 0) {
        count = $("input:checkbox[id^='checkbox']:checked").length;
        if (count == 0) {
            $.alert("请选择要删除的数据", 2);
            return;
        } else {
            roadLineId = "";
            $("input:checkbox[id^='checkbox']:checked").each(function () {
                roadLineId += $(this).val() + ",";
            });
            roadLineId = roadLineId.substring(0, roadLineId.length - 1);
        }
    }
    bootbox.confirm({
        buttons: {
            confirm: {
                label: '<i class="fa fa-check"></i> 确定',
                className: 'btn btn-primary btn-flat'
            },
            cancel: {
                label: '<i class="fa fa-close"></i> 取消',
                className: 'btn btn-warning btn-flat pull-left'
            }
        },
        message: '<i class="fa fa-question-circle-o"></i> <b class="customer-title">确定删除该' + count + '条数据吗?</b>',
        callback: function (result) {
            if (result) {
                $.ajax({
                    url: '../rds_roadLineController/deleteRoadLine',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        "roadLineIds": roadLineId
                    },
                    success: function (msg) {
                        if (msg != null && typeof (msg) != "undefined") {
                            if (msg.isSuccess == true) {
                                $.alert("数据删除成功", 1);
                                gMainDT.ajax.reload(null, false);
                            } else {
                                $.alert("数据删除失败", 2);
                            }
                        } else {
                            $.alert('系统错误，请联系管理员', 2);
                        }
                    },
                    error: function () {
                        $.alert('系统错误，请联系管理员', 2);
                    }
                });
            }
        }
    });
};


$("#roadLineType").change(function () {
    $("#roadLineGraphic").html("");
    setHtml($(this).val() - 0);

});

function setHtml(type) {
    switch (type) {
        case 0:
            type = "指示";
            appendHtml(type);
            break;
        case 1:
            type = "禁止";
            appendHtml(type);
            break;
        case 2:
            type = "警告";
            appendHtml(type);
            break;
        case 3:
        case 4:
            console.log("应藏")
            $("#graphic").fadeOut();
            break;
    }
}

function appendHtml(type) {
    $("#graphic").fadeIn();
    $("#roadLineGraphic").html(
        '<option value="0">纵向' + type + '标线</option>' +
        '<option value="1">横向' + type + '标线</option>' +
        '<option value="2">其他' + type + '标线</option>'
    );
}


/************图片轮转实现1*************************/
function next() {
    var $picList = $(".pic_list");
    var length = $picList.find("img").length;
    if (dex == length - 1)
        dex = 0;
    else
        dex += 1;
    $picList.css("top", -280 * dex + "px");
}

function prev() {
    var $picList = $(".pic_list");
    var length = $picList.find("img").length;
    if (dex == 0)
        dex = length - 1;
    else
        dex -= 1;
    $picList.css("top", -280 * dex + "px");
}

$(".next").click(_debounce(next, 200));

$(".prev").click(_debounce(prev, 200));

/************图片轮转实现2*************************/
function next2() {
    var $imgList = $(".img-content img[src]");
    $imgList.eq(dex).hide();
    if (dex == $imgList.length - 1)
        dex = 0;
    else dex++;
    $imgList.eq(dex).fadeIn()
}

function prev2() {
    var $imgList = $(".img-content img[src]");
    $imgList.eq(dex).hide();
    if (dex == 0)
        dex = $imgList.length - 1;
    else dex--;
    $imgList.eq(dex).fadeIn()
}

var $next2=_debounce(next2, 200)
var $prev2=_debounce(prev2, 200)

$(".next2").click($next2);

$(".prev2").click($prev2);

